package com.easy.work.function;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.WindowManager;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.base.syslib.util.AndroidUtils;
import com.easy.work.R;
import com.easy.work.app.BaseActivity;
import com.easy.work.utils.StringUtil;


/**
 * Created by zzgpc on 2017-10-15.
 */

public class WebViewAct extends BaseActivity {

    private WebView mWebView;
    private String urlStr;
    private String title = "易上班";
    private AnimationDrawable mAnimation;
    // private RelativeLayout layout_back;
    private FrameLayout frameLayout;
    boolean blockLoadingNetworkImage = false;
    ImageView img_loading;

    public static void start(Context context, String url, String title) {
        Intent intent = new Intent(context, WebViewAct.class);
        intent.putExtra("url", url);
        intent.putExtra("title", title);
        context.startActivity(intent);
    }

    public void onCreate(Bundle savedInstanceState) {
        isSetBatteryBar = true;
        super.onCreate(savedInstanceState);
        setContentView(R.layout.act_webview);
        if (AndroidUtils.getAndroidVersion() >= 19) {
            getWindow().setFlags(WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED, WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED);
        }

        if (getIntent().hasExtra("title")) {
            title = getIntent().getStringExtra("title");
            if (StringUtil.isEmpty("title")) {
                title = "易上班";
            }

        }
        initTitle(title);
        urlStr = getIntent().getStringExtra("url");
//        title = getIntent().getStringExtra("title");
//        initTitle(title);
        img_loading = (ImageView) findViewById(R.id.img_loading);
        // layout_back = (RelativeLayout) findViewById(R.id.layout_back);
        frameLayout = (FrameLayout) findViewById(R.id.webview_fram);
//        RelativeLayout rlBack = (RelativeLayout) findViewById(R.id.rl_back);
//        rlBack.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                finish();
//            }
//        });
        /*
         * layout_back.setOnClickListener(new OnClickListener() {
		 *
		 * @Override public void onClick(View v) { // TODO Auto-generated method
		 * stub finish(); } });
		 */

        mWebView = (WebView) findViewById(R.id.qq_mywebview);
        mWebView.setVerticalScrollBarEnabled(true);
        mWebView.setHorizontalScrollBarEnabled(false);
        mWebView.getSettings().setRenderPriority(WebSettings.RenderPriority.HIGH);
        mWebView.getSettings().setBlockNetworkImage(true);
        mWebView.getSettings().setSupportZoom(true);
        mWebView.getSettings().setBuiltInZoomControls(true);
        blockLoadingNetworkImage = true;
        mWebView.getSettings().setDomStorageEnabled(true);
        // 去掉缓存
        mWebView.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT);
        // mWebView.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
        mWebView.getSettings().setJavaScriptEnabled(true);
        mWebView.getSettings().setSupportZoom(true);
        mWebView.getSettings().setBuiltInZoomControls(false);
        // webSettings.setLayoutAlgorithm(LayoutAlgorithm.SINGLE_COLUMN);
        mWebView.requestFocus();

        mWebView.loadUrl(urlStr);

        WebChromeClient wvcc = new WebChromeClient() {

            @Override
            public void onReceivedTitle(WebView view, String title) {

                // TODO Auto-generated method stub
                super.onReceivedTitle(view, title);

            }


            @Override
            public void onProgressChanged(WebView view, int newProgress) {

                // TODO Auto-generated method stub
                super.onProgressChanged(view, newProgress);
                if (blockLoadingNetworkImage) {
                    mWebView.getSettings().setBlockNetworkImage(false);
                    blockLoadingNetworkImage = false;
                }

                if (newProgress == 100) {
                    stopAmin(img_loading);
                }

            }

        };
        // 设置setWebChromeClient对象
        mWebView.setWebChromeClient(wvcc);

        // 设置Web视图
        mWebView.setWebViewClient(new HelloWebViewClient());

        // img_service_catolog
        // ImageView img_sc = (ImageView)
        // findViewById(R.id.img_service_catolog);
        ;
        // img_sc.setOnClickListener(new View.OnClickListener() {
        // @Override
        // public void onClick(View arg0) {
        // finish();
        // }
        // });

//        startAmin(img_loading);
    }


    // 设置回退
    // 覆盖Activity类的onKeyDown(int keyCoder,KeyEvent event)方法
    public boolean onKeyDown(int keyCode, KeyEvent event) {

//        if ((keyCode == KeyEvent.KEYCODE_BACK) && mWebView.canGoBack()) {
//            mWebView.goBack(); // goBack()表示返回WebView的上一页面
//            return true;
//        } else {
//            finish();
//        }
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            finish();
            return true;
        }
        return true;
    }


    // Web视图
    private class HelloWebViewClient extends WebViewClient {

        public boolean shouldOverrideUrlLoading(WebView view, String url) {

            view.loadUrl(url);
            return true;
        }


        @Override
        public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {

            // TODO Auto-generated method stub
            super.onReceivedError(view, errorCode, description, failingUrl);

        }

    }


//    public void startAmin(View view) {
//
//        // Animation operatingAnim = AnimationUtils.loadAnimation(this,
//        // R.anim.refresh_data);
//        // LinearInterpolator lin = new LinearInterpolator();
//        // operatingAnim.setInterpolator(lin);
//        // view.startAnimation(operatingAnim);
//        Animation hyperspaceJumpAnimation = AnimationUtils.loadAnimation(this, R.drawable.frame);
//        view.setAnimation(hyperspaceJumpAnimation);
////		view.setBackgroundResource( R.anim.frame );
//        // 通过ImageView对象拿到背景显示的AnimationDrawable
//        mAnimation = (AnimationDrawable) view.getBackground();
//        // 为了防止在onCreate方法中只显示第一帧的解决方案之一
//        view.post(new Runnable() {
//
//            @Override
//            public void run() {
//
//                mAnimation.start();
//
//            }
//        });
//
//    }


    public void stopAmin(View view) {

        view.clearAnimation();
        view.setVisibility(View.GONE);
    }


    protected void onResume() {

        super.onResume();
        // if (mWebView.getProgress() < 100)

        // showDialog(PROGRESS_DIALOG_CONNECTING);
    }


    protected void onDestroy() {

        mWebView.stopLoading();
        frameLayout.removeView(mWebView);
        mWebView.removeAllViews();
        mWebView.destroy();
        super.onDestroy();
    }


    public void back(View view) {

        if (mWebView.canGoBack()) {
            mWebView.goBack(); // goBack()表示返回WebView的上一页面
        } else {
            finish();
        }
    }

}
